﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Enterprise.UI.Web.Modules.WebEmail
{
    public partial class Inbox : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindEmail();
            }
        }

        private void BindEmail()
        {
            Enterprise.Service.Email.EmailService es = new Service.Email.EmailService();
            string email = es.GetEmailAddressByUserId(Enterprise.Infrastructure.Utility.Get_UserId);
            System.Data.DataSet ds = es.GetMails(email);
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }

        protected void btn_Delete_Click(object sender, EventArgs e)
        {           
            Enterprise.Service.Email.EmailService es = new Service.Email.EmailService();
            es.Delete(GetSelectedMessages());
            BindEmail();
        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[5].Visible = false;
            if (e.Row.Cells[6].Text == "0")
            {
                e.Row.Font.Bold = true;                
            }
            e.Row.Cells[6].Visible = false;
        }

        protected void btn_Readed_Click(object sender, EventArgs e)
        {
            Enterprise.Service.Email.EmailService es = new Service.Email.EmailService();
            es.SetReaded(GetSelectedMessages());
            BindEmail();
        }

        /// <summary>
        /// 获取选中的messageId
        /// </summary>
        /// <returns></returns>
        private List<string> GetSelectedMessages()
        {
            List<string> MessageIds = new List<string>();
            foreach (GridViewRow row in GridView1.Rows)
            {
                CheckBox cb = (CheckBox)row.Cells[0].FindControl("cb_select");
                if (cb.Checked)
                {
                    MessageIds.Add(row.Cells[5].Text.ToString());
                }
            }
            return MessageIds;
        }
    }
}